package ccgl.com.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 拦截器
 * 
 * @author Administrator
 * 
 */
public class UrlFilter implements Filter {

	/**
	 * 不用检查的URL
	 */
	private List<String> noCheckURLList = new ArrayList<String>();
	/**
	 * 过滤不通过返回的页面
	 */
	private String redirectURL = null;

	/**
	 * Session秘钥
	 */

	/**
	 * destory
	 */
	public void destroy() {
	}

	/**
	 * Filter初始化
	 */
	public void init(FilterConfig filterConfig) throws ServletException {
		/**
		 * 暂时做成配置，后面从数据库里面做
		 */
		redirectURL = filterConfig.getInitParameter("redirectURL");
		String noCheckURLListStr = filterConfig
				.getInitParameter("noCheckURLList");
		if (noCheckURLListStr != null) {
			StringTokenizer st = new StringTokenizer(noCheckURLListStr, ";");
			noCheckURLList.clear();
			while (st.hasMoreTokens()) {
				noCheckURLList.add(st.nextToken());
			}
		}
	}

	/**
	 * 拦截方法
	 */
	public void doFilter(ServletRequest servletRequest,
			ServletResponse servletResponse, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		request.setCharacterEncoding("UTF-8");
		if (!this.checkNotRequestFilter(request)) {
			if (!this.checkCookies(request) || !this.checkSessions(request)) {
				System.out.println("拦截路径："
						+ request.getServletPath()
						+ (request.getPathInfo() == null ? "" : request
								.getPathInfo()));
				response.sendRedirect(request.getContextPath() + redirectURL);
			}
		}
		chain.doFilter(request, response);
	}

	/**
	 * 不需校验的过滤请求
	 * 
	 * @param request
	 * @return
	 */
	private boolean checkNotRequestFilter(HttpServletRequest request) {
		String uri = request.getServletPath()
				+ (request.getPathInfo() == null ? "" : request.getPathInfo());
		if (uri.contains("/images/") || uri.contains("/js/")
				|| uri.contains("/css/")) {
			return true;
		}
		return noCheckURLList.contains(uri);
	}

	/**
	 * 校验Cookie
	 * 
	 * @param request
	 *            请求对象
	 * @return 返回是否校验成功（true-成功）
	 */
	private boolean checkCookies(HttpServletRequest request) {
		Cookie[] cookies = request.getCookies();

		return true;
	}

	/**
	 * 校验session
	 * 
	 * @param request
	 *            请求对象
	 * @return 返回是否校验成功（true-成功）
	 */
	private boolean checkSessions(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Object user = session.getAttribute("user");
		return user != null;
	}
}
